package com.wnhz.work.yz.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.wnhz.work.yz.R;
import com.wnhz.work.yz.utils.Analysis;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

public class SelectImageActivity extends Activity implements View.OnClickListener {
    /**拍照,相册,取消*/
    private Button bt_carmal,bt_myphoto,bt_mycancel;
    /**上传的图片 */
    private File mCurrentPhotoFile;
    /* 裁剪后bitmap */
    private Bitmap crop_bitmap;
    /**返回数据解析提示对话框*/
    private Analysis analysis;
    private LinearLayout ll_my_image;
    private Intent intent;
    /*相机拍照文件名*/
    private String filename;
    /*相机拍照文件全路径*/
    private File outputImage;
    Uri imageUri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_image);

        /** 使窗口充满屏幕 */
        getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);

        initView();
        initListener();
        initStart();
    }

    private void initView (){
        bt_carmal= (Button) findViewById(R.id.bt_carmal);
        bt_myphoto= (Button) findViewById(R.id.bt_myphoto);
        bt_mycancel= (Button) findViewById(R.id.bt_mycancel);
        ll_my_image= (LinearLayout) findViewById(R.id.ll_my_image);
    }
    private void initListener (){
        bt_carmal.setOnClickListener(this);
        bt_myphoto.setOnClickListener(this);
        bt_mycancel.setOnClickListener(this);
        ll_my_image.setOnClickListener(this);
    }
    private void initStart (){
        analysis=new Analysis(SelectImageActivity.this);

        //图片名称 时间命名
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        Date date = new Date(System.currentTimeMillis());
        filename = format.format(date);
        //创建File对象用于存储拍照的图片 SD卡根目录
//        File outputImage = new File(Environment.getExternalStorageDirectory(),"test.jpg");
        //存储至DCIM文件夹
        String path = Environment.getExternalStorageDirectory()+"/DCIM/";
        outputImage = new File(path+filename+".jpg");
        //将File对象转换为Uri并启动照相程序
        imageUri = Uri.fromFile(outputImage);
    }
    /**
     * 从相册中选择
     */
    protected void doPickPhotoFromGallery () {
        try {
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
            intent.setType("image/*");
//            intent.putExtra("crop", "true");
//            intent.putExtra("aspectX", 1);
//            intent.putExtra("aspectY", 1);
//            intent.putExtra("outputX", 150);
//            intent.putExtra("outputY", 150);
//            intent.putExtra("return-data", true);
            startActivityForResult(intent, 111);
        } catch (Exception e) {
        }
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt_carmal:
                intent = new Intent("android.media.action.IMAGE_CAPTURE"); //照相
                intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY,1);//指定图片输出地址
                startActivityForResult(intent,222); //启动照相
                break;
            case R.id.bt_myphoto:
                doPickPhotoFromGallery();
                break;
            case R.id.bt_mycancel:
                finish();
                break;
            default:break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode != RESULT_OK){
            return;
        }
        switch (requestCode) {
            case 111:
                Uri uri = data.getData();
                intent = new Intent();
                intent.putExtra("uri",uri.toString());
                setResult(RESULT_OK,intent);
                finish();
                break;
            case 222:
                intent = new Intent();
                intent.putExtra("uri",imageUri.toString());
                setResult(RESULT_OK,intent);
                finish();
                break;

        }

    }
}
